package xin.ryven.workflow.service.impl;

import org.springframework.stereotype.Service;
import xin.ryven.workflow.model.FormDefinition;
import xin.ryven.workflow.repository.FormDefinitionRepository;
import xin.ryven.workflow.service.FormDefinitionService;

@Service
public class FormDefinitionServiceImpl implements FormDefinitionService {
    private final FormDefinitionRepository formDefinitionRepository;

    public FormDefinitionServiceImpl(FormDefinitionRepository formDefinitionRepository) {
        this.formDefinitionRepository = formDefinitionRepository;
    }

    @Override
    public FormDefinition getFormDefinition(String processKey) {
        return formDefinitionRepository.findByProcessKey(processKey)
                .orElse(null);
    }

    @Override
    public FormDefinition saveFormDefinition(String processKey, String formSchema) {
        FormDefinition formDefinition = formDefinitionRepository.findByProcessKey(processKey)
                .orElse(new FormDefinition());
        
        formDefinition.setProcessKey(processKey);
        formDefinition.setFormSchema(formSchema);
        
        return formDefinitionRepository.save(formDefinition);
    }
} 